package com.ltmonitor.web.controller.command;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.BasicData;
import com.ltmonitor.entity.MediaItem;
import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.service.IMediaItemService;
import com.ltmonitor.service.JT808Constants;
import com.ltmonitor.util.DateUtil;
import com.ltmonitor.web.util.JsonMessage;

/**
 * 下发媒体文件列表检索或媒体上传的指令
 * 
 * @author DELL
 * 
 */
@Controller
@RequestMapping("/media")
public class MediaAction extends TerminalCommandAction {

	@Autowired
	private IMediaItemService mediaItemService;
	
	/**
	 * 进入多媒体文件检索页面
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewList.action")
	public String view(long vehicleId, Model m)
	{
		m.addAttribute("vehicleId", vehicleId);
		return "/command/mediaList";
	}

	/**
	 * 进入多媒体文件提取页面
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewUpload.action")
	public String viewUpload(long vehicleId, Model m)
	{
		m.addAttribute("vehicleId", vehicleId);
		return "/command/mediaUpload";
	}

	/**
	 * 向终端发送多媒体记录上传指令
	 * @param vehicleId
	 * @param mediaType // 媒体类型 0：图像；1：音频；2：视频
	 * @param channel 通道
	 * @param eventType  事件类型 0：平台下发指令；1：定时动作；2：抢劫报警触发；3：碰撞侧翻报警触发；其他保留
	 * @param startDate 要查询的多媒体记录的开始日期和结束日期
	 * @param endDate 
	 * @param saveType 删除标志 BYTE 0：保留；1：删除；
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/upload.action")
	public JsonMessage upload(int vehicleId,int mediaType,int channel,int eventType,Date startDate,Date endDate,int saveType) {

		try {
			StringBuilder sb = new StringBuilder();
			sb.append(mediaType).append(";").append(channel).append(";")
					.append(eventType).append(";")
					.append(DateUtil.datetimeToString(startDate)).append(';')
					.append(DateUtil.datetimeToString(endDate)).append(';')
					.append(saveType);

			TerminalCommand tc = new TerminalCommand();
			tc.setCmdType(JT808Constants.CMD_MEDIA_UPLOAD);
			tc.setCmdData(sb.toString());
			tc.setCmd("" + mediaType);
			tc.setVehicleId(vehicleId);
			SendCommand(tc);

			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			return json(false, ex.getMessage());
		}
	}

	/**
	 * 单挑多媒体上传
	 * @param mediaId  单挑多媒体的记录Id
	 * @param saveType 删除标志 BYTE 0：保留；1：删除；
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/singleUpload.action")
	public JsonMessage singleUpload(int vehicleId,int mediaId,int saveType) {
		try {
			StringBuilder sb = new StringBuilder();
			sb.append(mediaId).append(";").append(saveType);

			TerminalCommand tc = new TerminalCommand();
			tc.setCmdType(JT808Constants.CMD_MEDIA_UPLOAD_SINGLE);
			tc.setCmdData(sb.toString());
			tc.setVehicleId(vehicleId);
			// tc.setCmd("" + getMediaType());
			SendCommand(tc);

			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			return json(false, ex.getMessage());
		}
	}

	/**
	 *  多媒体文件列表检索
	 * @param vehicleId
	 * @param mediaType
	 * @param channel
	 * @param eventType
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/search.action")
	public JsonMessage  search(int vehicleId,int mediaType,int channel,int eventType,Date startDate,Date endDate) {

		try {
			StringBuilder sb = new StringBuilder();
			sb.append(mediaType).append(';').append(channel).append(';')
					.append(eventType).append(';')
					.append(DateUtil.datetimeToString(startDate)).append(';')
					.append(DateUtil.datetimeToString(endDate));

			TerminalCommand tc = new TerminalCommand();
			tc.setCmdType(JT808Constants.CMD_MEDIA_SEARCH);
			tc.setCmdData(sb.toString());
			tc.setCmd("" + mediaType);
			tc.setVehicleId(vehicleId);
			SendCommand(tc);

			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}


	/**
	 * 根据指令ID，获取终端返回的多媒体记录数据列表
	 * @param commandId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryMediaInfo.action")
	public JsonMessage queryMediaInfo(long commandId) {
		try {
			TerminalCommand tc = getTerminalCommand(commandId);
			BasicData bd = getBasicDataService().getBasicDataByCode(
					tc.getStatus(), "TCommandStatus");
			String status = bd != null ? bd.getName() : "";
			Map result = new HashMap();
			result.put("status", status);
			if (TerminalCommand.STATUS_SUCCESS.equals(tc.getStatus())) {
				List<MediaItem> data = this.mediaItemService.queryMediaItemListByCommandId(commandId);
				result.put("data", data);
				return json(true, result);
			} else if (TerminalCommand.STATUS_NEW.equals(tc.getStatus()) == false
					&& TerminalCommand.STATUS_PROCESSING.equals(tc.getStatus()) == false) {
				return json(true, status);
			} else {
				return json(false, status);
			}
		} catch (Exception ex) {
			log.error(ex.getMessage());
			log.error(ex.getStackTrace());
			return json(false, "查询时发送错误:" + ex.getMessage());
		}
	}


}
